package net.cooltain.warhammer.running.entity;

import java.util.Map;

import net.cooltain.utils.Logger;
import net.cooltain.warhammer.framework.constant.ConstantPhases;
import net.cooltain.warhammer.framework.constant.ConstantPlayZone;
import net.cooltain.warhammer.running.start.GameBoardDouble;
import net.cooltain.warhammer.running.window.MainFrame;

/**
 * 玩家的游戏区
 * @author cooltain
 *
 */
public class MotionPlayerZone {
	
	public long             randomKey ;                 // 唯一标识
	public String           playerName ; 	            // 玩家姓名
	public boolean          isLogger         = true ;   // 是否输出日志
	
	public GameBoardDouble  gameBoard ;                 // 游戏主界面
	public MotionPlayerZone operatePlayer ;             // 对手
	public MainFrame        mainFrame;                  // 显示界面
	
	private Integer         capitalRaceType  ;          // 首都类型
	public Integer          raceInitFlag = 1;           // 首都初始旗帜数
	
	public DeskZone         deskZone       = new DeskZone(this);// 套牌区
	public DiscardZone      discardPile    = new DiscardZone(this);// 弃卡区
	public HandZone         handZone       = new HandZone(this);// 手牌
	
	public BattleAreaZone   battleZone     = new BattleAreaZone(this);// 战场区卡牌
	public KingdomAreaZone  kingdomZone    = new KingdomAreaZone(this);// 王国区卡牌
	public QuestAreaZone    questZone      = new QuestAreaZone(this);// 任务区卡牌
	public CapitalAreaZone  capitalZone    = new CapitalAreaZone(this);// 首都区卡牌
	public Map<Integer,ZoneEntity> allZoneMap = null;
	
	public MotionPlayerStatus status ;                  // 所有状态
	public Integer            currentTurnCount = 0;       // 当前回合数
	private Integer           resource         = 0;       // 资源数
	
	public boolean          isFisrtPlay      = false;   // 是否是先手(先手玩家跳过任务阶段与战场阶段)
	public boolean          isMyTurn         = false;   // 是否你的回合
	public int              allowDeveloperSet= 0 ;      // 本回合允许被打出的设施的数量
	
	public Integer          currentPhases    = -1;      // 所处的阶段
	public Integer          currentPhasesBattle    = -1;// 当前为战场阶段的哪一个阶段
	
	public Integer          attackToZone ;              // 被选为进攻的区域（战场/王国/任务)  
	
	/** 允许在自己回合打出设施卡,默认true */
	public boolean          allowDeveloperMyTurn = true;
	/** 允许在对方回合打出设施卡,默认false */
	public boolean          allowDeveloperOperateTurn = false;
	/** 允许打出设施卡的回合,默认只有首都阶段true */
	public Map<Integer,Boolean> allowDeveloperPhases = ConstantPhases.initAllowDeveloperPhases();
	
	/**
	 * 创建用户
	 * @param playerName
	 * @param raceType
	 */
	public MotionPlayerZone(String playerName,Integer capitalRaceType){
		this.randomKey = (long) (Math.random()*1000000);
		this.capitalRaceType = capitalRaceType;
		this.playerName = playerName ;
		MotionPlayerStatus.initStatus(this);
		this.allZoneMap = ConstantPlayZone.initAllPlayZone(this);
	}
	

	
	
	
	/**
	 * 玩家获胜了(当对手输了的时候会触发)
	 */
	public void playerWinner(){
		if(isLogger){
			Logger.info(new StringBuffer()
				.append(playerName).append(" 嬴入这场比赛")
			);
		}
	}
	
	
	/**
	 * 玩家放弃了比赛
	 */
	public void playerGiveUp(){
		if(isLogger){
			Logger.info(playerName+" 放弃了比赛");
		}
		this.operatePlayer.playerWinner();
	}
	
	/**
	 * 玩家输了
	 */
	public void playerLoser(){
		if(isLogger){
			Logger.info(playerName+" 输入这场比赛");
		}
		this.operatePlayer.playerWinner();
	}
	
	//==========================================================================
	//各个回合
	
	/**
	 * 切换到阶段0:回合开始,获得控制权
	 */
	public void toPhases0BegeningOfTurn(){
		this.currentTurnCount ++ ;
		this.operatePlayer.currentTurnCount ++ ;
		this.allowDeveloperSet = 1 ;
		if(isLogger){
			Logger.info("\n===="+" 回合"+this.currentTurnCount+"开始 ====");
		}
		this.currentPhases = ConstantPhases.BEGENING_OF_TURN;
		this.isMyTurn = true;
		// TODO : 激活只在这阶段才触发的效果
	}
	
	/**
	 * 切换到阶段1:王国阶段,获取资源
	 */
	public void toPhases1KingdomPhases(){
		if(!this.isMyTurn){
			if(isLogger){
				Logger.warn("现在是"+this.operatePlayer.playerName+"的回合,"+playerName+"不能操作.");
			}
			return;
		}
		if(this.currentPhases==ConstantPhases.KINGDOM_PHASES){
			if(isLogger){
				Logger.warn("已经是"+playerName+"的王国阶段");
			}
			return;
		}
		if(isLogger){
			Logger.info("~~~~"+playerName+" 王国阶段~~~~");
		}
		this.currentPhases = ConstantPhases.KINGDOM_PHASES;
		this.setResource(0);
		// TODO : 提醒玩家复元一个腐化单位
		
		// TODO : 激活只在这阶段才触发的效果
		gainResourcesAtKingdomPhases();
	}
	
	
	/**
	 * 阶段1其它:王国阶段获取资源
	 */
	private void gainResourcesAtKingdomPhases(){
		int allCount = this.kingdomZone.getPowerSum();
		if(isLogger){
			Logger.info(new StringBuffer()
				.append(playerName+" 在王国阶段获得了"+allCount+"个资源.")
			);
		}
		this.setResource(allCount);
	}
	

	/**
	 * 切换到阶段2:任务阶段,抽取卡片
	 */
	public void toPhases2QuestPhases(){
		if(!this.isMyTurn){
			if(isLogger){
				Logger.warn(new StringBuffer()
					.append("现在是"+this.operatePlayer.playerName+"的回合,")
					.append(playerName+"不能操作.")
				);
			}
			return;
		}
		if(this.currentPhases==ConstantPhases.QUEST_PHASES){
			if(isLogger){
				Logger.warn(new StringBuffer()
					.append("已经是"+playerName+"的任务阶段")
				);
			}
			return;
		}
		if(isLogger){
			Logger.info("~~~~"+playerName+" 任务阶段~~~~");
		}
		this.currentPhases = ConstantPhases.QUEST_PHASES;
		if(this.isFisrtPlay){
			if(isLogger){
				Logger.info("由于"+playerName+"是先手,所以跳过任务阶段.");
			}
			toPhases3CapitalPhases();
			return;
		}
		drawAtQuestPhases();
	}
	
	
	/**
	 * 阶段2:任务阶段抽牌
	 */
	private void drawAtQuestPhases(){
		int allCount = this.questZone.getPowerSum();
		boolean tmpStatus = this.isLogger ;
		this.isLogger = false;
		this.deskZone.drawTopCard(allCount);
		if(tmpStatus){
			Logger.info(new StringBuffer()
				.append(playerName).append(" 在任务阶段抽了").append(allCount).append("张牌.")
				.append("(手:").append(this.handZone.getCardSize())
				.append("|牌:").append(this.deskZone.getCardSize()).append(")")
			);
		}
		this.isLogger = tmpStatus ;
	}
	
	/**
	 * 切换到阶段3:首都阶段,放置卡片
	 */
	public void toPhases3CapitalPhases(){
		if(!this.isMyTurn){
			if(isLogger){
				Logger.warn("现在是"+this.operatePlayer.playerName+"的回合,"+playerName+"不能操作.");
			}
			return;
		}
		if(this.currentPhases==ConstantPhases.CAPITAL_PHASES){
			if(isLogger){
				Logger.warn("已经是"+playerName+"的首都阶段");
			}
			return;
		}
		if(isLogger){
			Logger.info("~~~~"+playerName+" 首都阶段~~~~");
		}
		this.currentPhases = ConstantPhases.CAPITAL_PHASES;
		// TODO 
	}
	
	/**
	 * 切换到阶段4:战场阶段
	 */
	public void toPhases4BattleFieldPhases(){
		if(!this.isMyTurn){
			if(isLogger){
				Logger.warn("现在是"+this.operatePlayer.playerName+"的回合,"+playerName+"不能操作.");
			}
			return;
		}
		if(this.currentPhases==ConstantPhases.BATTLEFIELD_PHASES){
			if(isLogger){
				Logger.warn("已经是"+playerName+"的战场阶段");
			}
			return;
		}
		if(isLogger){
			Logger.info("~~~~"+playerName+" 战场阶段~~~~");
		}
		this.currentPhases = ConstantPhases.BATTLEFIELD_PHASES;
		if(this.isFisrtPlay){
			if(isLogger){
				Logger.info("由于"+playerName+"是先手,所以跳过战场阶段.");
			}
			toPhases5EndOfTurn();
			return;
		}
		this.toPhases4Battle1ChooseZone();
	}
	
	/**
	 * 切换到阶段4-1:战场阶段-选择进攻区域
	 */
	public void toPhases4Battle1ChooseZone(){
		if(!this.isMyTurn){
			if(isLogger){
				Logger.warn("现在是对方的回合,"+playerName+"不能操作.");
			}
			return;
		}
		if(this.currentPhases!=ConstantPhases.BATTLEFIELD_PHASES){
			if(isLogger){
				Logger.error("非法操作");
			}
			return;
		}
		if(isLogger){
			Logger.info("~~~~"+playerName+" 选择要进攻的区域~~~~");
		}
		this.currentPhasesBattle = ConstantPhases.BATTLE1_CHOOSE_AREA;
		// TODO 
	}
	
	/**
	 * 切换到阶段4-2:战场阶段-攻方选择进攻部队
	 */
	public void toPhases4Battle2ChooseAttackUnit(){
		if(!this.isMyTurn){
			if(isLogger){
				Logger.warn("现在是对方的回合,"+playerName+"不能操作.");
			}
			return;
		}
		if(this.currentPhases!=ConstantPhases.BATTLEFIELD_PHASES){
			if(isLogger){
				Logger.error("非法操作");
			}
			return;
		}
		if(!this.operatePlayer.battleZone.isBeAttacked() 
			&&!this.operatePlayer.kingdomZone.isBeAttacked() 
			&&!this.operatePlayer.questZone.isBeAttacked() ){
			if(isLogger){
				Logger.warn("还未选择进攻区域");
			}
			this.currentPhasesBattle = ConstantPhases.BATTLE1_CHOOSE_AREA;
			return ;
		}
		
		if(isLogger){
			Logger.info("~~~~"+playerName+" 从战场区选择要进攻的部队~~~~");
		}		
		this.currentPhasesBattle = ConstantPhases.BATTLE2_CHOOSE_ATTACK;
	}
	
	/**
	 * 切换到阶段4-3:战场阶段-守方选择防守部队
	 */
	public void toPhases4Battle3ChooseDefendUnit(){
//		if(!this.isMyTurn){
//			if(isLogger){
//				Logger.warn("现在是对方的回合,"+playerName+"不能操作.");
//			}
//			return;
//		}
		if(this.currentPhases!=ConstantPhases.BATTLEFIELD_PHASES){
			if(isLogger){
				Logger.error("非法操作");
			}
			return;
		}
		if(isLogger){
			Logger.info("~~~~"+this.operatePlayer.playerName+" 选择要防守的部队~~~~");
		}
		this.currentPhasesBattle = ConstantPhases.BATTLE3_CHOOSE_DEFEND;
	}
	
	/**
	 * 切换到阶段4-4:战场阶段-攻方分配伤害
	 */
	public void toPhases4Battle4ChooseAttackDamage(){
		if(!this.isMyTurn){
			if(isLogger){
				Logger.warn("现在是对方的回合,"+playerName+"不能操作.");
			}
			return;
		}
		if(this.currentPhases!=ConstantPhases.BATTLEFIELD_PHASES){
			if(isLogger){
				Logger.error("非法操作");
			}
			return;
		}
		if(isLogger){
			Logger.info("~~~~"+this.playerName+" 分配伤害到对方守方部队身上~~~~");
		}
		this.currentPhasesBattle = ConstantPhases.BATTLE4_ATTACK_DAMEGE;
	}
	
	
	/**
	 * 切换到阶段4-5:战场阶段-守方分配伤害
	 */
	public void toPhases4Battle5ChooseDefendDamage(){
//		if(!this.isMyTurn){
//			if(isLogger){
//				Logger.warn("现在是对方的回合,"+playerName+"不能操作.");
//			}
//			return;
//		}
		if(this.currentPhases!=ConstantPhases.BATTLEFIELD_PHASES){
			if(isLogger){
				Logger.error("非法操作");
			}
			return;
		}
		if(isLogger){
			Logger.info("~~~~"+this.operatePlayer.playerName+" 分配伤害到对方攻方部队身上~~~~");
		}
		this.currentPhasesBattle = ConstantPhases.BATTLE5_DEFEND_DAMEGE;
	}
	
	/**
	 * 切换到阶段4-6:战场阶段-开始计算双方伤害
	 */
	public void toPhases4Battle6CalculateDamage(){
		if(!this.isMyTurn){
			if(isLogger){
				Logger.warn("现在是对方的回合,"+playerName+"不能操作.");
			}
			return;
		}
		if(this.currentPhases!=ConstantPhases.BATTLEFIELD_PHASES){
			if(isLogger){
				Logger.error("非法操作");
			}
			return;
		}
		if(isLogger){
			Logger.info("~~~~ 系统自动分配攻守双方伤害 ~~~~");
		}
		this.currentPhasesBattle = ConstantPhases.BATTLE6_CALC_DAMEGE;
		// TODO:
	}
	
	/**
	 * 切换到阶段5:回合结束
	 */
	public void toPhases5EndOfTurn(){
		if(!this.isMyTurn){
			if(isLogger){
				Logger.warn("现在是"+this.operatePlayer.playerName+"的回合,"+playerName+"不能操作.");
			}
			return;
		}
		if(this.currentPhases==ConstantPhases.END_OF_TURN_PHASES){
			if(isLogger){
				Logger.info("已经是"+playerName+"的回合结束阶段");
			}
			return;
		}
		if(isLogger){
			Logger.info("~~~~"+playerName+" 回合结束~~~~");
		}
		this.currentPhases = ConstantPhases.END_OF_TURN_PHASES;
		// TODO : 激活只在这阶段才触发的效果
		this.isMyTurn = false;
		this.operatePlayer.toPhases0BegeningOfTurn();
		this.isFisrtPlay = false;
	}
	
	
	//==========================================================================
	// Get & Set 方法
	
	public Integer getResource() {
		return resource;
	}
	
	public void setResource(Integer resource) {
		this.resource = resource;
	}

	public Integer getCapitalRaceType() {
		return capitalRaceType;
	}

	public void setCapitalRaceType(Integer capitalRaceType) {
		this.capitalRaceType = capitalRaceType;
	}
	
	/**
	 * 获得全场指定种族的旗帜总数(包含首都的1个旗帜)
	 * @param raceType
	 * @return
	 */
	public int getTeamRaceFlag(Integer raceType){
		int count = 0;
		if(raceType==this.capitalRaceType){
			count += this.raceInitFlag;
		}
		count += this.battleZone.getRaceTypeCount(raceType);
		count += this.kingdomZone.getRaceTypeCount(raceType);
		count += this.questZone.getRaceTypeCount(raceType);
		return count;
	}

	//==========================================================================
	// 事件
	
	/** 增加资源 */
	public void eventAddResource(int number){
		this.resource += number;
	}
	
	/** 减少资源 */
	public void eventRemoveResource(int number){
		this.resource -= number;
		if(this.resource<0){
			this.resource = 0;
		}
	}
	
}

